package offertest.test04;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName MyBlockingQueue
 * @Description
 * @Author ZJX
 * @Date 2025/4/12 16:43
 * @Version 1.0
 **/
public class MyBlockingQueue {
    private String[] elem = null;
    int head = 0;
    int tail = 0;
    int size = 0;
    int capacity = 0;

    public MyBlockingQueue(int capacity){
        this.capacity = capacity;
        this.elem = new String[capacity];
    }

    public synchronized void put(String value) throws InterruptedException {
        if (size == capacity){
            wait();
        }

        elem[tail] = value;
        tail = (tail + 1) % capacity;
        size++;

        this.notify();
    }


    public synchronized String take() throws InterruptedException {
        while (size == 0){
            wait();
        }

        String value = elem[head];
        elem[head] = null;
        head = (head + 1) % capacity;
        size--;

        this.notify();

        return value;
    }




}
